package com.dongge.realm;

import com.dongge.service.UserService;
import org.apache.shiro.authc.*;
import org.apache.shiro.realm.Realm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

/**
 * 验证逻辑
 */
//@Repository("realm")
public class RealmLogin implements Realm {

    @Autowired
    private UserService userService;

    public String getName() {
        return "RealmLogin";
    }

    public boolean supports(AuthenticationToken authenticationToken) {
        return authenticationToken instanceof UsernamePasswordToken;
    }

    public AuthenticationInfo getAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        String username = (String)authenticationToken.getPrincipal();
        String password = new String((char[])authenticationToken.getCredentials());

        if(!userService.login(username,password)){
            throw new UnknownAccountException("用户名或者密码错误");
        }
        return new SimpleAuthenticationInfo(username,password,getName());
    }
}
